<template>
	<!-- 大图商品卡片 -->
	<view v-if="goodsList.length">
		<view class="sh-big-card-wrap ma20 " v-for="item in goodsList" :key="item.id">
			<view class="big-goods-card pb30" @tap="jump('/pages/goods/detail/index', { id: item.id })">
				<view class="img-wrap mb20">
					<image class="goods-img" :src="item.image" mode="aspectFill"></image>
					<image class="goods-tag" v-if="item.activity_status === 'waiting' || item.activity_status === 'ing'" :src="$IMG_URL + '/goods/limit_tag.png'" mode=""></image>
					<image class="goods-day" v-else-if="item.is_hot" :src="$IMG_URL + '/goods/goods_hot_tag.png'" mode=""></image>
					<image class="goods-next" v-else-if="item.is_next_day_delivery" :src="$IMG_URL + '/goods/day5.png'" mode=""></image>
				</view>
				<view class="goods-title px20 mb10 more-t">
					<!-- <text v-if="item.is_next_day_delivery == 1" style="color: #f5a623;">[次日达]</text> -->
					{{ item.title }}
				</view>
				<view class="goods-time px20 mb20">{{ item.subtitle }}</view>
				<view class="u-m-b-20 px20" v-if="item.activity_discounts_tags.length">
					<view class="tag-box  u-m-r-10" v-for="(tag, index) in item.activity_discounts_tags" :key="index">{{ tag }}</view>
				</view>

				<view class="goods-progress px20 x-f mb10">
					<view class="cu-progress round sm">
						<view class="progress--ing" :style="[{ width: getProgress(item.sales, item.stock) }]"></view>
						<view class="round-tag" :style="[{ left: getProgress(item.sales, item.stock) }]">
							<image class="round-tag-img" :src="$IMG_URL + '/goods/goods_mini_hot.png'" mode=""></image>
						</view>
					</view>
					<view class="googs-send-num ml20">已售{{ item.sales }}件</view>
				</view>
				<view class="x-bc px20">
					<view class="price-box x-f">
						<view class="price">{{ item.price }}</view>
						<view class="origin-price ml10">￥{{ item.original_price }}</view>
					</view>
					<view class="cart-box">
						<!-- 单规格 -->
						<view class="" v-if="!item.activity">
							<view v-if="!item.is_sku">
								<button v-if="!isCart(item.id)" @tap.stop="addCart(item.sku_price[0])" class="cu-btn cart-btn">加入购物车</button>
								<view class="num-step" v-else>
									<uni-number-box
										@change="onChangeNum($event, item.sku_price[0])"
										:value="checkCart[item.id].num"
										:step="1"
										:currentSkuPrice="item.sku_price[0]"
										:goodsInfo="item"
										:min="0"
										:disabled="numberDisabled"
									></uni-number-box>
								</view>
							</view>
							<!-- 多规格 -->
							<button class="cu-btn item-btn sel-sku " @tap.stop="selSku(item)" v-else>选规格</button>
						</view>
						<button class="cu-btn item-btn sel-sku" v-else>立即抢购</button>
					</view>
				</view>
			</view>
		</view>
		<button v-if="total > perPage" class="cu-btn refresh-btn my20 x-f" @tap.stop="loadMore">
			<text class="cuIcon-refresh" :class="{ 'refresh-active': isRefresh }"></text>
			{{ listParams.page >= lastPage ? '收起' : '加载更多' }}
		</button>
		<!-- 规格 -->
		<t-sku v-model="showSku" v-if="showSku && goodsInfo.id" :goodsInfo="goodsInfo" :buyType="'cart'"></t-sku>
	</view>
</template>

<script>
import { mapMutations, mapActions, mapState } from 'vuex';
export default {
	components: {},
	data() {
		return {
			listParams: {
				page: 1
			},
			lastPage: 1, //分页总数
			total: 0, //总商品数
			perPage: 0, //单页商品数
			goodsList: [],
			isRefresh: false,
			numberDisabled: false, //加入购物车
			showSku: false, //显示规格弹窗
			goodsInfo: {} //商品详情
		};
	},
	props: {
		detail: {
			type: Object,
			default: null
		},
		type: {
			type: String,
			default: ''
		}
	},
	computed: {
		...mapState({
			cartList: state => state.cart.cartList
		}),
		// 购物车检测
		checkCart() {
			let obj = {};
			this.cartList.forEach(item => {
				obj[item.goods_id] = {
					num: item.goods_num,
					cartOrderId: item.id
				};
			});
			return obj;
		}
	},
	created() {
		if (this.type) {
			this.getActivityGoodsList();
		} else {
			if (this.detail.id) {
				this.listParams.category_id = this.detail.id;
				this.getGoodsList();
			}
			if (this.detail.ids) {
				this.listParams.goods_ids = this.detail.ids;
				this.getGoodsList();
			}
		}
	},
	methods: {
		...mapActions(['getCartList', 'changeCartList', 'addCartGoods']),
		// 百分比
		getProgress(sales, stock) {
			let unit = '';
			if (stock + sales > 0) {
				let num = (sales / (sales + stock)) * 100;
				unit = num.toFixed(2) + '%';
			} else {
				unit = '0%';
			}
			return unit;
		},
		// 路由跳转
		jump(path, parmas) {
			this.$Router.push({
				path: path,
				query: parmas
			});
		},
		// 天数
		getDay(timestamp) {
			if (timestamp.toString().length == 10) timestamp *= 1000;
			return parseInt(timestamp / 1000 / 60 / 60 / 24);
		},
		// 检测是否为购物车商品
		isCart(id) {
			let goodsId = id + '';
			return Object.keys(this.checkCart).includes(goodsId);
		},

		// 检测商品在购物车中的下标
		checkGoodsIndex(id) {
			let cIndex = 0;
			this.cartList.forEach((item, index) => {
				if (id == item.goods_id) {
					cIndex = index;
				}
			});
			return cIndex;
		},
		// 更改商品数
		async onChangeNum(e, goods) {
			let gIndex = this.checkGoodsIndex(goods.goods_id);
			if (e != this.checkCart[goods.goods_id].num) {
				this.numberDisabled = true;
				uni.showLoading({
					mask: true
				});
				if (e <= 0) {
					await this.changeCartList({ ids: [this.checkCart[goods.goods_id].cartOrderId], art: 'delete' });
				} else {
					this.$set(this.cartList[gIndex], 'goods_num', +e);
					await this.changeCartList({
						ids: [this.checkCart[goods.goods_id].cartOrderId],
						goodsNum: +e,
						art: 'change'
					});
				}
				this.numberDisabled = false;
				await uni.hideLoading();
			}
		},

		// 加入购物车
		addCart(goods) {
			if (goods.stock <= 0) {
				this.$tools.toast('库存不足');
				return;
			}
			let obj = {
				goods_id: goods.goods_id,
				goods_num: 1,
				sku_price_id: goods.id,
				goods_price: goods.price
			};
			let confirmGoodsList = {
				list: [obj],
				from: 'goods'
			};
			this.addCartGoods(confirmGoodsList).then(res => {
				if (res.code === 1) {
					this.$tools.toast(res.msg);
				}
			});
		},

		// 添加购物车,多规格
		async selSku(info) {
			this.goodsInfo = {};
			this.getGoodsDetail(info.id);
			this.showSku = true;
		},

		// 商品详情
		getGoodsDetail(id) {
			let that = this;
			that.$api('goods.detail', {
				id: id
			}).then(res => {
				if (res.code === 1) {
					that.goodsInfo = res.data;
				}
			});
		},

		// 商品列表
		getGoodsList() {
			let that = this;
			that.$api('goods.lists', this.listParams).then(res => {
				if (res.code === 1) {
					this.lastPage = res.data.last_page;
					this.total = res.data.total;
					this.perPage = res.data.per_page;
					this.isRefresh = false;
					that.goodsList = [...that.goodsList, ...res.data.data];
				}
			});
		},
		// 活动商品列表
		getActivityGoodsList() {
			let that = this;
			that.$api('goods.activity', {
				...{ activity_id: that.detail.id },
				...this.listParams
			}).then(res => {
				this.lastPage = res.data.last_page;
				this.total = res.data.total;
				this.perPage = res.data.per_page;
				this.isRefresh = false;
				that.goodsList = [...that.goodsList, ...res.data.goods.data];
			});
		},

		// 加载更多
		loadMore() {
			if (!this.isRefresh) {
				// 加载更多
				if (this.listParams.page < this.lastPage) {
					this.isRefresh = true;
					this.listParams.page += 1;
					this.getGoodsList();
				} else {
					// 重置为1页数据
					this.listParams.page = 1;
					this.lastPage = 1;
					this.goodsList = [];
					this.getGoodsList();
				}
			}
		}
	}
};
</script>

<style lang="scss">
.big-goods-card {
	background-color: #fff;
	border-radius: 20rpx;
	box-shadow: 0 0 8rpx 2rpx rgba(199, 199, 199, 0.3);
	.img-wrap {
		position: relative;
		overflow: hidden;
		height: 360rpx;
		width: 100%;
		.goods-img {
			width: 100%;
			height: 360rpx;
			border-radius: 20rpx 20rpx 0 0;
			background-color: #ccc;
		}
		.goods-tag {
			position: absolute;
			top: 16rpx;
			left: 16rpx;
			width: 88rpx;
			height: 88rpx;
		}
		.goods-next {
			position: absolute;
			top: 16rpx;
			right: 16rpx;
			width: 88rpx;
			height: 88rpx;
		}
		
		.goods-day {
			position: absolute;
			bottom: -18rpx;
			left: 1rpx;
			width: 120rpx;
			height: 100rpx;
			z-index: 2;
		}
	}
	.goods-title {
		font-size: 30rpx;
		font-weight: bold;
		color: #333333;
		line-height: 36rpx;
	}
	.goods-time {
		font-size: 24rpx;
		font-weight: 400;
		color: #999999;
		line-height: 36rpx;
	}
	.tag-box {
		border: 1px solid #ff0000;
		display: inline-block;
		font-size: 20rpx;
		line-height: 30rpx;
		padding: 0 10rpx;
		color: #ff0000;
		border-radius: 8rpx;
	}
	.goods-progress {
		.cu-progress {
			width: 200rpx;
			height: 20rpx;
			background: rgba(#ff6361, 0.2);
			overflow: visible;
			position: relative;
			.progress--ing {
				background: #ff6361;
				border-radius: 20rpx;
			}
			.round-tag {
				position: absolute;
				bottom: 0;
				width: 20rpx;
				height: 28rpx;
				margin-left: -10rpx;
				.round-tag-img {
					width: 20rpx;
					height: 28rpx;
				}
			}
		}
		.googs-send-num {
			font-size: 24rpx;
			font-weight: 400;
			color: #bebebe;
		}
	}
	.price-box {
		vertical-align: text-bottom;
		.price {
			font-size: 36rpx;
			color: $t-priceColor;
			font-weight: 600;
			&::before {
				content: '￥';
				font-size: 28rpx;
			}
			vertical-align: text-bottom;
		}
		.origin-price {
			vertical-align: text-bottom;
			font-size: 26rpx;
			font-weight: 400;
			text-decoration: line-through;
			color: #c4c4c4;
		}
	}
	.cart-box {
		.cart-btn,
		.sel-sku {
			width: 172rpx;
			height: 58rpx;
			background: linear-gradient(to right, $t-gradualColor, $t-mainColor);
			border-radius: 30rpx;
			padding: 0;
			font-size: 26rpx;
			font-weight: 500;
			color: #ffffff;
		}
	}
}
</style>
